<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.2.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/blog/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/blog/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/blog/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/blog/images/logo.svg" color="#222">

<link rel="stylesheet" href="/blog/css/main.css">


<link rel="stylesheet" href="/blog/lib/font-awesome/css/font-awesome.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"hubei2017.gitee.io","root":"/blog/","scheme":"Mist","version":"7.7.1","exturl":false,"sidebar":{"position":"right","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":false,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}}};
  </script>

  <meta name="description" content="1.概览2.进程和线程 进程和线程的概念  并行和并发的概念  2.1进程和线程的对比 线程是进程的基本执行单元，一个进程的所有任务都在线程中执行 进程要想执行任务，必须得有线程，进程至少要有一条线程 线程更轻量，线程上下文切换成本一般上要比进程上下文切换低  2.2 并行与并发 单核cpu下，线程实际还是 串行执行 的。操作系统中有一个组件叫做任务调度器，将 cpu 的时间（windows 下时">
<meta property="og:type" content="article">
<meta property="og:title" content="7.juc">
<meta property="og:url" content="http://hubei2017.gitee.io/blog/2021/04/15/7-juc/index.html">
<meta property="og:site_name" content="益达的博客">
<meta property="og:description" content="1.概览2.进程和线程 进程和线程的概念  并行和并发的概念  2.1进程和线程的对比 线程是进程的基本执行单元，一个进程的所有任务都在线程中执行 进程要想执行任务，必须得有线程，进程至少要有一条线程 线程更轻量，线程上下文切换成本一般上要比进程上下文切换低  2.2 并行与并发 单核cpu下，线程实际还是 串行执行 的。操作系统中有一个组件叫做任务调度器，将 cpu 的时间（windows 下时">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="http://hubei2017.gitee.io/blog/2021/04/15/7-juc/1584358565127.png">
<meta property="og:image" content="http://hubei2017.gitee.io/blog/2021/04/15/7-juc/1584359104448.png">
<meta property="article:published_time" content="2021-04-15T10:17:32.000Z">
<meta property="article:modified_time" content="2021-09-03T01:59:12.399Z">
<meta property="article:author" content="益达">
<meta property="article:tag" content="juc">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://hubei2017.gitee.io/blog/2021/04/15/7-juc/1584358565127.png">

<link rel="canonical" href="http://hubei2017.gitee.io/blog/2021/04/15/7-juc/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome: false,
    isPost: true
  };
</script>

  <title>7.juc | 益达的博客</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <div>
      <a href="/blog/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">益达的博客</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
        <p class="site-subtitle">记录生活中的点点滴滴</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>


<nav class="site-nav">
  
  <ul id="menu" class="menu">
        <li class="menu-item menu-item-home">

    <a href="/blog/" rel="section"><i class="fa fa-fw fa-home"></i>首页</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/blog/about/" rel="section"><i class="fa fa-fw fa-user"></i>关于</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/blog/tags/" rel="section"><i class="fa fa-fw fa-tags"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/blog/categories/" rel="section"><i class="fa fa-fw fa-th"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/blog/archives/" rel="section"><i class="fa fa-fw fa-archive"></i>归档</a>

  </li>
        <li class="menu-item menu-item-schedule">

    <a href="/blog/schedule/" rel="section"><i class="fa fa-fw fa-calendar"></i>日程表</a>

  </li>
        <li class="menu-item menu-item-sitemap">

    <a href="/blog/sitemap.xml" rel="section"><i class="fa fa-fw fa-sitemap"></i>站点地图</a>

  </li>
  </ul>

</nav>
</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content">
            

  <div class="posts-expand">
      
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block " lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://hubei2017.gitee.io/blog/2021/04/15/7-juc/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/blog/images/avatar.gif">
      <meta itemprop="name" content="益达">
      <meta itemprop="description" content="直到这一刻微笑着说话为止，我至少留下了一公升眼泪">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="益达的博客">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          7.juc
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2021-04-15 18:17:32" itemprop="dateCreated datePublished" datetime="2021-04-15T18:17:32+08:00">2021-04-15</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2021-09-03 09:59:12" itemprop="dateModified" datetime="2021-09-03T09:59:12+08:00">2021-09-03</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/blog/categories/juc/" itemprop="url" rel="index"><span itemprop="name">juc</span></a>
                </span>
            </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h2 id="1-概览"><a href="#1-概览" class="headerlink" title="1.概览"></a>1.概览</h2><h2 id="2-进程和线程"><a href="#2-进程和线程" class="headerlink" title="2.进程和线程"></a>2.进程和线程</h2><ul>
<li>进程和线程的概念 </li>
<li>并行和并发的概念</li>
</ul>
<h4 id="2-1进程和线程的对比"><a href="#2-1进程和线程的对比" class="headerlink" title="2.1进程和线程的对比"></a>2.1进程和线程的对比</h4><ul>
<li>线程是进程的基本执行单元，一个进程的所有任务都在线程中执行</li>
<li>进程要想执行任务，必须得有线程，进程至少要有一条线程</li>
<li>线程更轻量，线程上下文切换成本一般上要比进程上下文切换低</li>
</ul>
<h4 id="2-2-并行与并发"><a href="#2-2-并行与并发" class="headerlink" title="2.2 并行与并发"></a>2.2 并行与并发</h4><ul>
<li><p>单核cpu下，线程实际还是 串行执行 的。操作系统中有一个组件叫做任务调度器，将 cpu 的时间（windows 下时间片最小约为 15 毫秒）分给不同的程序使用，只是由于 cpu 在线程间（时间片很短）的切换非常快，人类感觉是 同时运行的 。总结为一句话就是： <strong>微观串行，宏观并行</strong> ， 一般会将这种 线程轮流使用 CPU 的做法称为并发， concurrent 。</p>
</li>
<li><p>多核 cpu下，每个 核（core） 都可以调度运行线程，这时候线程可以是并行的。 </p>
</li>
<li><p>并发（concurrent）是同一时间应对（dealing with）多件事情的能力 </p>
</li>
<li><p>并行（parallel）是同一时间动手做（doing）多件事情的能力 </p>
</li>
</ul>
<h2 id="3-Java-线程"><a href="#3-Java-线程" class="headerlink" title="3. Java 线程"></a>3. Java 线程</h2><h4 id="3-1-创建和运行线程"><a href="#3-1-创建和运行线程" class="headerlink" title="3.1 创建和运行线程"></a>3.1 创建和运行线程</h4><ul>
<li><p>方法一，直接使用 Thread ：重写run方法</p>
</li>
<li><p>方法二，使用 Runnable 配合 Thread ：调用Runnable里面的run方法</p>
</li>
<li><p>方法三，FutureTask 配合 Thread</p>
</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">@Test</span><br><span class="line">public void testFutureTask() throws Exception&#123;</span><br><span class="line">    FutureTask&lt;Integer&gt; task &#x3D; new FutureTask&lt;&gt;(new Callable&lt;Integer&gt;() &#123;</span><br><span class="line">        @Override</span><br><span class="line">        public Integer call() throws Exception &#123;</span><br><span class="line">            log.info(&quot;futureTask running....&quot;);</span><br><span class="line">            Thread.sleep(2000);</span><br><span class="line">            return 11;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;);</span><br><span class="line"></span><br><span class="line">    Thread thread &#x3D; new Thread(task, &quot;t1&quot;);</span><br><span class="line">    thread.start();</span><br><span class="line">    log.info(&quot;result,&#123;&#125;&quot;,task.get());</span><br><span class="line">    Thread.sleep(3000);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="3-2-查看进程线程的方法"><a href="#3-2-查看进程线程的方法" class="headerlink" title="3.2 查看进程线程的方法"></a>3.2 查看进程线程的方法</h4><p><strong>linux ：</strong></p>
<ul>
<li>ps -fe 查看所有进程 </li>
<li>ps -fT -p <PID> 查看某个进程（PID）的所有线程 </li>
<li>kill 杀死进程 </li>
<li>top 按大写 H 切换是否显示线程 </li>
<li>top -H -p <PID> 查看某个进程（PID）的所有线程 </li>
</ul>
<p><strong>Java ：</strong></p>
<ul>
<li>jps 命令查看所有 Java 进程 </li>
<li>jstack <PID> 查看某个 Java 进程（PID）的所有线程状态 </li>
<li>jconsole 来查看某个 Java 进程中线程的运行情况（图形界面）</li>
</ul>
<h4 id="3-3线程的运行原理"><a href="#3-3线程的运行原理" class="headerlink" title="3.3线程的运行原理"></a>3.3线程的运行原理</h4><p><strong>栈与栈帧 ：</strong></p>
<p>​    我们都知道 JVM 中由堆、栈、方法区所组成，其中栈内存是给谁用的呢？ 其实就是线程，每个线程启动后 虚拟<br>机就会为其分配一块栈内存 。</p>
<ul>
<li>每个栈由多个栈帧（Frame）组成，对应着每次方法调用时所占用的内存 </li>
<li>每个线程只能有一个活动栈帧，对应着当前正在执行的那个方法 </li>
</ul>
<p>代码：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">TestFrames</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        Thread t1 = <span class="keyword">new</span> Thread()&#123;</span><br><span class="line">            <span class="meta">@Override</span></span><br><span class="line">            <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">run</span><span class="params">()</span> </span>&#123;</span><br><span class="line">                method1(<span class="number">20</span>);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;;</span><br><span class="line">        t1.setName(<span class="string">&quot;t1&quot;</span>);</span><br><span class="line">        t1.start();</span><br><span class="line">        method1(<span class="number">10</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">method1</span><span class="params">(<span class="keyword">int</span> x)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span> y = x + <span class="number">1</span>;</span><br><span class="line">        Object m = method2();</span><br><span class="line">        System.out.println(m);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">static</span> Object <span class="title">method2</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        Object n = <span class="keyword">new</span> Object();</span><br><span class="line">        <span class="keyword">return</span> n;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><img src="1584358565127.png" alt="1584358565127"></p>
<ul>
<li>程序计数器是记录jvm指令执行的</li>
<li>每个栈由多个栈帧（Frame）组成，对应着每次方法调用时所占用的内存 </li>
<li>每个线程只能有一个活动栈帧，对应着当前正在执行的那个方法 </li>
<li>栈帧里面主要有局部变量表，方法参数，返回地址，锁记录，操作数栈.</li>
<li>活动栈帧执行完成后会弹出</li>
</ul>
<p><strong>线程上下文切换（Thread Context Switch）</strong>：</p>
<p>​    因为以下一些原因导致 cpu 不再执行当前的线程，转而执行另一个线程的代码 </p>
<ul>
<li>线程的 cpu 时间片用完 </li>
<li>垃圾回收 </li>
<li>有更高优先级的线程需要运行 </li>
<li>线程自己调用了 sleep、yield、wait、join、park、synchronized、lock 等方法 </li>
</ul>
<p>当 Context Switch 发生时，需要由操作系统保存当前线程的状态，并恢复另一个线程的状态，Java 中对应的概念就是程序计数器（Program Counter Register），它的作用是记住下一条 jvm 指令的执行地址，是线程私有的     </p>
<ul>
<li>状态包括程序计数器、虚拟机栈中每个栈帧的信息，如局部变量、操作数栈、返回地址等 </li>
<li>Context Switch 频繁发生会影响性能 </li>
</ul>
<h4 id="3-4常用方法"><a href="#3-4常用方法" class="headerlink" title="3.4常用方法"></a>3.4常用方法</h4><p><img src="1584359104448.png" alt="1584359104448"></p>
<h4 id="3-5start-与-run"><a href="#3-5start-与-run" class="headerlink" title="3.5start 与 run"></a>3.5start 与 run</h4><ul>
<li>直接调用 run 是在主线程中执行了 run，没有启动新的线程 </li>
<li>使用 start 是启动新的线程，通过新的线程间接执行 run 中的代码 </li>
</ul>
<h4 id="3-6-sleep-与-yield"><a href="#3-6-sleep-与-yield" class="headerlink" title="3.6 sleep 与 yield"></a>3.6 sleep 与 yield</h4><p><strong>sleep</strong></p>
<ul>
<li>调用 sleep 会让当前线程从 <em>Running</em> 进入 <em>Timed Waiting</em> 状态（阻塞） </li>
<li>其它线程可以使用 interrupt 方法打断正在睡眠的线程，这时 sleep 方法会抛出 InterruptedException </li>
<li>睡眠结束后的线程未必会立刻得到执行 </li>
<li>建议用 TimeUnit 的 sleep 代替 Thread 的 sleep 来获得更好的可读性 </li>
</ul>
<p><strong>yield</strong> </p>
<ul>
<li>调用 yield 会让当前线程从 <em>Running</em> 进入 <em>Runnable</em> 就绪状态，然后调度执行其它线程 </li>
<li>具体的实现依赖于操作系统的任务调度器 （如果当前状态下没有别的线程，那么当前cpu还是会让当前线程执行，想让没让出去）</li>
</ul>
<p>区别：状态不一样（sleep有休眠时间，yield没有）</p>
<p><strong>线程优先级</strong> ：</p>
<ul>
<li>线程优先级会提示（hint）调度器优先调度该线程，但它仅仅是一个提示，调度器可以忽略它 </li>
<li>如果 cpu 比较忙，那么优先级高的线程会获得更多的时间片，但 cpu 闲时，优先级几乎没作用 </li>
</ul>
<p><strong>sleep 实现</strong> ：</p>
<p>防止cpu占用100%</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">while</span>(<span class="keyword">true</span>) &#123;</span><br><span class="line">	<span class="keyword">try</span> &#123;</span><br><span class="line">				Thread.sleep(<span class="number">50</span>);</span><br><span class="line">		&#125; <span class="keyword">catch</span> (InterruptedException e) &#123;</span><br><span class="line">				e.printStackTrace();</span><br><span class="line">		&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li>可以用 wait 或 条件变量达到类似的效果 </li>
<li>不同的是，后两种都需要加锁，并且需要相应的唤醒操作，一般适用于要进行同步的场景 </li>
<li>sleep 适用于无需锁同步的场景 </li>
</ul>
<h4 id="3-7-join-方法详解"><a href="#3-7-join-方法详解" class="headerlink" title="3.7 join 方法详解"></a>3.7 join 方法详解</h4><p>t.join();      //使调用线程 t 在此之前执行完毕。</p>
<p>t.join(1000);  //等待 t 线程，等待时间是1000毫秒</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">join</span><span class="params">()</span> <span class="keyword">throws</span> InterruptedException </span>&#123;</span><br><span class="line">    log.debug(<span class="string">&quot;开始....&quot;</span>);</span><br><span class="line"></span><br><span class="line">    Thread t1 = <span class="keyword">new</span> Thread(() -&gt; &#123;</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            Thread.sleep(<span class="number">3000</span>);</span><br><span class="line">        &#125; <span class="keyword">catch</span> (InterruptedException e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;,<span class="string">&quot;t1&quot;</span>);</span><br><span class="line">    t1.start();</span><br><span class="line">    Thread t2 = <span class="keyword">new</span> Thread(() -&gt; &#123;</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            Thread.sleep(<span class="number">5000</span>);</span><br><span class="line">        &#125; <span class="keyword">catch</span> (InterruptedException e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;,<span class="string">&quot;t1&quot;</span>);</span><br><span class="line">    t2.start();</span><br><span class="line">    t1.join();</span><br><span class="line">    t2.join();</span><br><span class="line">    log.debug(<span class="string">&quot;结束....&quot;</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="3-8-interrupt-方法详解"><a href="#3-8-interrupt-方法详解" class="headerlink" title="3.8 interrupt 方法详解"></a>3.8 interrupt 方法详解</h4><p>打断 sleep，wait，join 的线程 </p>
<p>这几个方法都会让线程进入阻塞状态 </p>
<p><strong>打断 sleep 的线程, 会清空打断状态，以 sleep 为例</strong> </p>

    </div>

    
    
    

      <footer class="post-footer">
          <div class="post-tags">
              <a href="/blog/tags/juc/" rel="tag"># juc</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/blog/2021/04/15/6-jvm%E9%9D%A2%E8%AF%95/" rel="prev" title="6.jvm面试">
      <i class="fa fa-chevron-left"></i> 6.jvm面试
    </a></div>
      <div class="post-nav-item">
    <a href="/blog/2021/04/15/8-juc%E9%9D%A2%E8%AF%95/" rel="next" title="8.juc面试">
      8.juc面试 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  

  </div>


          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let activeClass = CONFIG.comments.activeClass;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E6%A6%82%E8%A7%88"><span class="nav-number">1.</span> <span class="nav-text">1.概览</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E8%BF%9B%E7%A8%8B%E5%92%8C%E7%BA%BF%E7%A8%8B"><span class="nav-number">2.</span> <span class="nav-text">2.进程和线程</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#2-1%E8%BF%9B%E7%A8%8B%E5%92%8C%E7%BA%BF%E7%A8%8B%E7%9A%84%E5%AF%B9%E6%AF%94"><span class="nav-number">2.0.1.</span> <span class="nav-text">2.1进程和线程的对比</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-2-%E5%B9%B6%E8%A1%8C%E4%B8%8E%E5%B9%B6%E5%8F%91"><span class="nav-number">2.0.2.</span> <span class="nav-text">2.2 并行与并发</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-Java-%E7%BA%BF%E7%A8%8B"><span class="nav-number">3.</span> <span class="nav-text">3. Java 线程</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#3-1-%E5%88%9B%E5%BB%BA%E5%92%8C%E8%BF%90%E8%A1%8C%E7%BA%BF%E7%A8%8B"><span class="nav-number">3.0.1.</span> <span class="nav-text">3.1 创建和运行线程</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-2-%E6%9F%A5%E7%9C%8B%E8%BF%9B%E7%A8%8B%E7%BA%BF%E7%A8%8B%E7%9A%84%E6%96%B9%E6%B3%95"><span class="nav-number">3.0.2.</span> <span class="nav-text">3.2 查看进程线程的方法</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-3%E7%BA%BF%E7%A8%8B%E7%9A%84%E8%BF%90%E8%A1%8C%E5%8E%9F%E7%90%86"><span class="nav-number">3.0.3.</span> <span class="nav-text">3.3线程的运行原理</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-4%E5%B8%B8%E7%94%A8%E6%96%B9%E6%B3%95"><span class="nav-number">3.0.4.</span> <span class="nav-text">3.4常用方法</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-5start-%E4%B8%8E-run"><span class="nav-number">3.0.5.</span> <span class="nav-text">3.5start 与 run</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-6-sleep-%E4%B8%8E-yield"><span class="nav-number">3.0.6.</span> <span class="nav-text">3.6 sleep 与 yield</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-7-join-%E6%96%B9%E6%B3%95%E8%AF%A6%E8%A7%A3"><span class="nav-number">3.0.7.</span> <span class="nav-text">3.7 join 方法详解</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-8-interrupt-%E6%96%B9%E6%B3%95%E8%AF%A6%E8%A7%A3"><span class="nav-number">3.0.8.</span> <span class="nav-text">3.8 interrupt 方法详解</span></a></li></ol></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">益达</p>
  <div class="site-description" itemprop="description">直到这一刻微笑着说话为止，我至少留下了一公升眼泪</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/blog/archives/">
        
          <span class="site-state-item-count">17</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/blog/categories/">
          
        <span class="site-state-item-count">7</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
        <span class="site-state-item-count">1</span>
        <span class="site-state-item-name">标签</span>
      </div>
  </nav>
</div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">益达</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> 强力驱动 v5.2.0
  </div>
  <span class="post-meta-divider">|</span>
  <div class="theme-info">主题 – <a href="https://mist.theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Mist</a> v7.7.1
  </div>

        








      </div>
    </footer>
  </div>

  
  <script src="/blog/lib/anime.min.js"></script>

<script src="/blog/js/utils.js"></script>


<script src="/blog/js/schemes/muse.js"></script>


<script src="/blog/js/next-boot.js"></script>




  















  

  

</body>
</html>
